<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>activitymonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_activitymonitor.js.html">activitymonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Activity Monitor.
<a name="line17"></a> *
<a name="line18"></a> * Fires throttled events when a user interacts with the specified document.
<a name="line19"></a> * This class also exposes the amount of time since the last user event.
<a name="line20"></a> *
<a name="line21"></a> * If you would prefer to get BECOME_ACTIVE and BECOME_IDLE events when the
<a name="line22"></a> * user changes states, then you should use the IdleTimer class instead.
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.ui.ActivityMonitor&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom&#39;);
<a name="line28"></a>goog.require(&#39;goog.events&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line30"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Once initialized with a document, the activity monitor can be queried for
<a name="line35"></a> * the current idle time.
<a name="line36"></a> * TODO: Expand this class to allow it to monitor multiple DOMs.
<a name="line37"></a> *
<a name="line38"></a> * @param {goog.dom.DomHelper|Array.&lt;goog.dom.DomHelper&gt;} opt_domHelper
<a name="line39"></a> *     DomHelper which contains the document(s) to listen to.  If null, the
<a name="line40"></a> *     default document is usedinstead.
<a name="line41"></a> *
<a name="line42"></a> * @constructor
<a name="line43"></a> * @extends {goog.events.EventTarget}
<a name="line44"></a> */
<a name="line45"></a>goog.ui.ActivityMonitor = function(opt_domHelper) {
<a name="line46"></a>  goog.events.EventTarget.call(this);
<a name="line47"></a>
<a name="line48"></a>  var documents;
<a name="line49"></a>  if (!opt_domHelper) {
<a name="line50"></a>    documents = [goog.dom.getDomHelper().getDocument()];
<a name="line51"></a>  } else if (goog.isArray(opt_domHelper)) {
<a name="line52"></a>    documents = [];
<a name="line53"></a>    for (var i = 0; i &lt; opt_domHelper.length; i++) {
<a name="line54"></a>       documents.push(opt_domHelper[i].getDocument());
<a name="line55"></a>    }
<a name="line56"></a>  } else {
<a name="line57"></a>    documents = [opt_domHelper.getDocument()];
<a name="line58"></a>  }
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The document body which is being listened to.
<a name="line62"></a>   * @type {Array.&lt;HTMLDocument&gt;}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.documents_ = documents;
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The time (in milliseconds) of the last user event.
<a name="line69"></a>   * @type {number}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.lastEventTime_ = goog.now();
<a name="line73"></a>
<a name="line74"></a>  var eventHandler = new goog.events.EventHandler(this);
<a name="line75"></a>  /**
<a name="line76"></a>   * The event handler.
<a name="line77"></a>   * @type {goog.events.EventHandler}
<a name="line78"></a>   * @private
<a name="line79"></a>   */
<a name="line80"></a>  this.eventHandler_ = eventHandler;
<a name="line81"></a>
<a name="line82"></a>  // Set up listeners on capture
<a name="line83"></a>  for (var i = 0; i &lt; this.documents_.length; i++) {
<a name="line84"></a>    eventHandler.listen(
<a name="line85"></a>        this.documents_[i], goog.ui.ActivityMonitor.userEventTypesDocuments_,
<a name="line86"></a>        this.handleEvent_, true);
<a name="line87"></a>    eventHandler.listen(
<a name="line88"></a>        this.documents_[i].body, goog.ui.ActivityMonitor.userEventTypesBody_,
<a name="line89"></a>        this.handleEvent_, true);
<a name="line90"></a>  }
<a name="line91"></a>};
<a name="line92"></a>goog.inherits(goog.ui.ActivityMonitor, goog.events.EventTarget);
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * The last event type that was detected.
<a name="line97"></a> * @type {string}
<a name="line98"></a> * @private
<a name="line99"></a> */
<a name="line100"></a>goog.ui.ActivityMonitor.prototype.lastEventType_ = &#39;&#39;;
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * The mouse x-position after the last user event.
<a name="line105"></a> * @type {number}
<a name="line106"></a> * @private
<a name="line107"></a> */
<a name="line108"></a>goog.ui.ActivityMonitor.prototype.lastMouseX_;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * The mouse y-position after the last user event.
<a name="line113"></a> * @type {number}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.ui.ActivityMonitor.prototype.lastMouseY_;
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * The earliest time that another throttled ACTIVITY event will be dispatched
<a name="line121"></a> * @type {number}
<a name="line122"></a> * @private
<a name="line123"></a> */
<a name="line124"></a>goog.ui.ActivityMonitor.prototype.minEventTime_ = 0;
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Minimum amount of time in ms between throttled ACTIVITY events
<a name="line129"></a> * @type {number}
<a name="line130"></a> */
<a name="line131"></a>goog.ui.ActivityMonitor.MIN_EVENT_SPACING = 3 * 1000;
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * If a user executes one of these events, s/he is considered not idle.
<a name="line136"></a> * @type {Array.&lt;goog.events.EventType&gt;}
<a name="line137"></a> * @private
<a name="line138"></a> */
<a name="line139"></a>goog.ui.ActivityMonitor.userEventTypesBody_ =
<a name="line140"></a>  [goog.events.EventType.CLICK, goog.events.EventType.DBLCLICK,
<a name="line141"></a>   goog.events.EventType.MOUSEDOWN, goog.events.EventType.MOUSEUP,
<a name="line142"></a>   goog.events.EventType.MOUSEMOVE];
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * If a user executes one of these events, s/he is considered not idle.
<a name="line147"></a> * @type {Array.&lt;goog.events.EventType&gt;}
<a name="line148"></a> * @private
<a name="line149"></a> */
<a name="line150"></a>goog.ui.ActivityMonitor.userEventTypesDocuments_ =
<a name="line151"></a>  [goog.events.EventType.KEYDOWN, goog.events.EventType.KEYUP];
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Event constants for the activity monitor.
<a name="line156"></a> * @enum {string}
<a name="line157"></a> */
<a name="line158"></a>goog.ui.ActivityMonitor.Event = {
<a name="line159"></a>  /** Event fired when the user does something interactive */
<a name="line160"></a>  ACTIVITY: &#39;activity&#39;
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/** @inheritDoc */
<a name="line165"></a>goog.ui.ActivityMonitor.prototype.disposeInternal = function() {
<a name="line166"></a>  goog.ui.ActivityMonitor.superClass_.disposeInternal.call(this);
<a name="line167"></a>  this.eventHandler_.dispose();
<a name="line168"></a>  this.eventHandler_ = null;
<a name="line169"></a>  delete this.documents_;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Updates the last event time when a user action occurs.
<a name="line175"></a> * @param {goog.events.BrowserEvent} e Event object.
<a name="line176"></a> * @private
<a name="line177"></a> */
<a name="line178"></a>goog.ui.ActivityMonitor.prototype.handleEvent_ = function(e) {
<a name="line179"></a>  var update = false;
<a name="line180"></a>  switch (e.type) {
<a name="line181"></a>    case goog.events.EventType.MOUSEMOVE:
<a name="line182"></a>      // In FF 1.5, we get spurious mouseover and mouseout events when the UI
<a name="line183"></a>      // redraws. We only want to update the idle time if the mouse has moved.
<a name="line184"></a>      if (typeof this.lastMouseX_ == &#39;number&#39; &amp;&amp;
<a name="line185"></a>          this.lastMouseX_ != e.clientX ||
<a name="line186"></a>          typeof this.lastMouseY_ == &#39;number&#39; &amp;&amp;
<a name="line187"></a>          this.lastMouseY_ != e.clientY) {
<a name="line188"></a>        update = true;
<a name="line189"></a>      }
<a name="line190"></a>      this.lastMouseX_ = e.clientX;
<a name="line191"></a>      this.lastMouseY_ = e.clientY;
<a name="line192"></a>      break;
<a name="line193"></a>    default:
<a name="line194"></a>      update = true;
<a name="line195"></a>  }
<a name="line196"></a>
<a name="line197"></a>  if (update) {
<a name="line198"></a>    this.updateIdleTime_(goog.now(), /** @type {string} */ (e.type));
<a name="line199"></a>  }
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Updates the last event time to be the present time, useful for non-DOM
<a name="line205"></a> * events that should update idle time.
<a name="line206"></a> */
<a name="line207"></a>goog.ui.ActivityMonitor.prototype.resetTimer = function() {
<a name="line208"></a>  this.updateIdleTime_(goog.now(), &#39;manual&#39;);
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Does the work of updating the idle time and firing an event
<a name="line214"></a> * @param {number} eventTime Time (in MS) of the event that cleared the idle
<a name="line215"></a> * timer.
<a name="line216"></a> * @param {string} eventType Type of the event, used only for debugging.
<a name="line217"></a> * @private
<a name="line218"></a> */
<a name="line219"></a>goog.ui.ActivityMonitor.prototype.updateIdleTime_ = function(eventTime,
<a name="line220"></a>      eventType) {
<a name="line221"></a>  // update internal state noting whether the user was idle
<a name="line222"></a>  this.lastEventTime_ = eventTime;
<a name="line223"></a>  this.lastEventType_ = eventType;
<a name="line224"></a>
<a name="line225"></a>  // dispatch event
<a name="line226"></a>  if (eventTime &gt; this.minEventTime_) {
<a name="line227"></a>    this.dispatchEvent(goog.ui.ActivityMonitor.Event.ACTIVITY);
<a name="line228"></a>    this.minEventTime_ = eventTime + goog.ui.ActivityMonitor.MIN_EVENT_SPACING;
<a name="line229"></a>  }
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * Returns the amount of time the user has been idle.
<a name="line235"></a> * @param {number} opt_now The current time can optionally be passed in for the
<a name="line236"></a> *     computation to avoid an extra Date allocation.
<a name="line237"></a> * @return {number} The amount of time in ms that the user has been idle.
<a name="line238"></a> */
<a name="line239"></a>goog.ui.ActivityMonitor.prototype.getIdleTime = function(opt_now) {
<a name="line240"></a>  var now = opt_now || goog.now();
<a name="line241"></a>  return now - this.lastEventTime_;
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>/**
<a name="line245"></a> * Returns the type of the last user event.
<a name="line246"></a> * @return {string} event type.
<a name="line247"></a> */
<a name="line248"></a>goog.ui.ActivityMonitor.prototype.getLastEventType = function() {
<a name="line249"></a>  return this.lastEventType_;
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Returns the time of the last event
<a name="line255"></a> * @return {number} last event time.
<a name="line256"></a> */
<a name="line257"></a>goog.ui.ActivityMonitor.prototype.getLastEventTime = function() {
<a name="line258"></a>  return this.lastEventTime_;
<a name="line259"></a>};
</pre>


</body>
</html>
